package br.com.automed.bean;

import java.io.Serializable;
import java.util.Map;

import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import br.com.automed.entity.MatriculaValida;
import br.com.automed.form.ManterPacienteForm;
import br.com.automed.jpautil.JPAUtil;
import br.com.automed.service.UsuarioBusiness;
import br.com.automed.util.Navegacao;
import br.com.automed.util.Util;

@Named(value="manterPacienteBean")
@RequestScoped
public class ManterPacienteBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2069942381069729068L;
	
	private ManterPacienteForm pacienteForm;
	private String parametro;
	
	public ManterPacienteBean() {
		pacienteForm = new ManterPacienteForm();
	}
	
	public String cadastrar(){
		
		String navegacao = null;
		try {
			
			MatriculaValida mv = UsuarioBusiness.verificaMatriculaPaciente(pacienteForm.getPaciente().getMatricula());
			
			FacesContext fc = FacesContext.getCurrentInstance();
			Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
			this.parametro = params.get("modal");
			
			if(!pacienteForm.getPaciente().getSenha().equals(pacienteForm.getPaciente().getConfirmaSenha())){
			
				FacesMessage message = new FacesMessage();
				message.setSummary(Util.getMessage("display.validator.senha", null));
				message.setSeverity(FacesMessage.SEVERITY_INFO);
				
				FacesContext.getCurrentInstance().addMessage("confirmaSenha", message);
				this.parametro = "false";
			}else if(mv == null || mv.getFlagAtivo().equals("B")){
				
				FacesMessage message = new FacesMessage();
				message.setSummary(Util.getMessage( (mv == null ? "display.validator.matricula" : (mv.getFlagAtivo().equals("B") ? "display.validator.matriculaBloqueada" : null)) , null));
				message.setSeverity(FacesMessage.SEVERITY_INFO);
				
				FacesContext.getCurrentInstance().addMessage("matricula", message);
				this.parametro = "false";
			}else if(!mv.getConvenio().getConvenio().equals(pacienteForm.getPaciente().getConvenio().getConvenio())){
				
				FacesMessage message = new FacesMessage();
				message.setSummary(Util.getMessage("display.validator.convenio", null));
				message.setSeverity(FacesMessage.SEVERITY_INFO);
				
				FacesContext.getCurrentInstance().addMessage("convenio", message);
				this.parametro = "false";
			}else{
				UsuarioBusiness.carregaDadosPaciente(pacienteForm);
				if(!JPAUtil.create(pacienteForm.getPaciente())){
					/**
					 * Verificar se der erro!
					 */
				}
				navegacao = Navegacao.HOME;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return navegacao;
		
	}
	
	public ManterPacienteForm getPacienteForm() {
		return pacienteForm;
	}

	public void setPacienteForm(ManterPacienteForm pacienteForm) {
		this.pacienteForm = pacienteForm;
	}

	public String getParametro() {
		return parametro;
	}

	public void setParametro(String parametro) {
		this.parametro = parametro;
	}

}
